package com.lzw.bluetoothpractice.Activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.lzw.bluetoothpractice.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {
    //设置这是三个导航
    private ViewPager viewPager;
    //存放这几个导航页
    private ArrayList<View> pageViews;
    //设置这是三个图片
    private ImageView imageView;
    //存放三个图片
    private ImageView[] imageViews;
    // 包裹滑动图片LinearLayout
    private ViewGroup main;
    // 包裹小圆点的LinearLayout
    private ViewGroup group;
    //点击体验按钮
    Button bt_tiyan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        LayoutInflater inflater = getLayoutInflater();
        // LayoutInflater这个类的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/
        // 下的xml布局文件，并且实例化；而findViewById()是找xml布局文件下的具体widget控件
        // (如Button、TextView等)。
        pageViews = new ArrayList<View>(); // 把item1-06.xml放入pageViews数组中，以方便下面把pageViews中的数据
        // 将三个界面加到pageViews里面，封装到GuidePageAdapter适配器中。
        pageViews.add(inflater.inflate(R.layout.daohang_item1, null));
        pageViews.add(inflater.inflate(R.layout.daohang_item2, null));
        pageViews.add(inflater.inflate(R.layout.daohang_item3, null));
        main = (ViewGroup) inflater.inflate(R.layout.activity_main, null); // 把main.xml加载进来
        group = (ViewGroup) main.findViewById(R.id.viewGroup);
        viewPager = (ViewPager) main.findViewById(R.id.guidePages);
        imageViews = new ImageView[pageViews.size()];
        for (int i = 0; i < pageViews.size(); i++) { // 把小圆点图片加载到LinearLayout中。
            imageView = new ImageView(MainActivity.this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
            imageView.setPadding(20, 0, 20, 0);
            imageViews[i] = imageView;
            if (i == 0) {
                // 默认选中第一张图，默认换台是选中第一张图
                imageViews[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            } else {
                imageViews[i].setBackgroundResource(R.mipmap.page_indicator);
            }
            group.addView(imageViews[i]);
        }
        setContentView(main);
        // 把适配器中的数据加载到viewPager中。
        viewPager.setAdapter(new GuidePageAdapter());
        // 当滑动中间的图片，对应的小圆点也要滑动
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
        bt_tiyan = (Button) this.findViewById(R.id.button_tiyan);
    }
    //对立即体验的监听

    public void buttonOnClick(View v){
        Intent intent = new Intent();
        //跳转到下一页
        intent.setClass(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    // 指引页面数据适配器
    class GuidePageAdapter extends PagerAdapter {
        @Override
        public int getCount() { // 此函数的作用类似于baseAdapter中的getCount()函数
            return pageViews.size();
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return super.getItemPosition(object);
        }
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }
        @Override // 此函数的作用类似于baseAdapter中的getView()函数
        public Object instantiateItem(View arg0, int arg1) { // arg0就是Viewpager对象
            // TODO Auto-generated method stub
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }
        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub
        }
        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub
        }
    }
    // 指引页面更改事件监听器
    class GuidePageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0].setBackgroundResource(R.mipmap.page_indicator_focused);
                if (arg0 != i) {
                    imageViews[i].setBackgroundResource(R.mipmap.page_indicator);
                }
            }
        }
    }
}

